package com.bmd.backgroundmanger.controller;

import com.bmd.backgroundmanger.commons.Result;
import com.bmd.backgroundmanger.entity.UserAnswer;
import com.bmd.backgroundmanger.service.IUserAnswerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户答题记录Controller
 */
@RestController
@RequestMapping("/userAnswer")
@Tag(name = "用户答题记录")
public class UserAnswerController {

    @Resource
    private IUserAnswerService userAnswerService;

    @PostMapping("/submitAnswer")
    @Operation(summary = "提交答案")
    public Result<String> submitAnswer(@RequestBody UserAnswer userAnswer) {
        System.out.println("=== 收到答案提交请求 ===");
        System.out.println("用户ID: " + userAnswer.getUserId());
        System.out.println("题目ID: " + userAnswer.getQuestionId());
        System.out.println("答案内容: " + userAnswer.getAnswerContent());
        System.out.println("请求对象: " + userAnswer);
        
        try {
            boolean success = userAnswerService.submitAnswer(userAnswer);
            System.out.println("Service返回结果: " + success);
            
            if (success) {
                System.out.println("答案提交成功!");
                return Result.success("答案提交成功!");
            } else {
                System.err.println("Service返回false，答案提交失败!");
                return Result.error(500, "答案提交失败!");
            }
        } catch (Exception e) {
            System.err.println("Controller层异常: " + e.getMessage());
            e.printStackTrace();
            return Result.error(500, "答案提交异常: " + e.getMessage());
        }
    }

    @GetMapping("/getUserAnswerHistory/{userId}")
    @Operation(summary = "获取用户答题历史")
    public Result<List<UserAnswer>> getUserAnswerHistory(@PathVariable Integer userId) {
        List<UserAnswer> answers = userAnswerService.getUserAnswerHistory(userId);
        return Result.success(answers);
    }

    @GetMapping("/getUserStats/{userId}")
    @Operation(summary = "获取用户答题统计")
    public Result<Map<String, Object>> getUserStats(@PathVariable Integer userId) {
        Map<String, Object> stats = new HashMap<>();
        Double correctRate = userAnswerService.getUserCorrectRate(userId);
        List<UserAnswer> answers = userAnswerService.getUserAnswerHistory(userId);
        
        stats.put("correctRate", correctRate);
        stats.put("totalAnswers", answers.size());
        stats.put("correctAnswers", answers.stream().filter(UserAnswer::getIsCorrect).count());
        
        return Result.success(stats);
    }

    @GetMapping("/getQuestionStats/{questionId}")
    @Operation(summary = "获取题目统计")
    public Result<Map<String, Object>> getQuestionStats(@PathVariable Integer questionId) {
        Map<String, Object> stats = new HashMap<>();
        Double correctRate = userAnswerService.getQuestionCorrectRate(questionId);
        stats.put("correctRate", correctRate);
        return Result.success(stats);
    }
} 